package org.skydiveapphd;

import java.util.List;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.Signature;
import org.skydiveapplibrary.repository.SignatureRepository;
import org.skydiveapplibrary.ui.DrawingView;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageButton;
import android.widget.TextView;

public class SignatureActivity extends Fragment
{
	// repositories
	private SignatureRepository repository;
	
	// listeners
	private OnClickListener doneListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			save();
		}
	};
	private OnClickListener cancelListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			cancel();
		}
	};
	private OnClickListener clearListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			clear();
		}
	};

	public static SignatureActivity newInstance(Bundle args) 
	{
		SignatureActivity f = new SignatureActivity();
		f.setArguments(args);
		return f;
	}
	
	@Override 
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    	repository = new SignatureRepository(getActivity());
    }
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    		Bundle savedInstanceState)
	{	
	    // Inflate the layout for this fragment
	    return inflater.inflate(R.layout.signature_view, container, false);   	
	}
	
	@Override
	public void onResume()
	{
    	super.onResume();
    	
    	// init header
    	TextView headerText = (TextView) getActivity().findViewById(R.id.FormHeaderTextView);
    	headerText.setText(R.string.signature_header_label);
    	
    	// add button listeners
    	ImageButton cancelButton = (ImageButton) getActivity().findViewById(R.id.SignatureCancelButton);
    	cancelButton.setOnClickListener(cancelListener);
    	
    	ImageButton doneButton = (ImageButton) getActivity().findViewById(R.id.SignatureSaveButton);
    	doneButton.setOnClickListener(doneListener);
    	
    	ImageButton clearButton = (ImageButton) getActivity().findViewById(R.id.SignatureClearButton);
    	clearButton.setOnClickListener(clearListener);

    	// setup autocomplete license field
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_dropdown_item_1line, repository.getSignatureLicenses());
        AutoCompleteTextView signatureLicenseField = (AutoCompleteTextView) getActivity().findViewById(R.id.SignatureLicenseField);
        signatureLicenseField.setAdapter(adapter);
	}
	
	private void save()
	{
		// get ui components
		DrawingView drawingView = (DrawingView) getActivity().findViewById(
				R.id.SignatureDrawingView);
		TextView licenseField = (TextView) getActivity().findViewById(
				R.id.SignatureLicenseField);

		// get signature data
		byte[] data = drawingView.getImageData();
		String license = licenseField.getText().toString();

		// get log entry ids
		List<Integer> logEntryIds = getArguments().getIntegerArrayList(
				Constants.EXTRA_SELECTED_LOG_ENTRY_IDS);

		// create signature
		Signature signature = new Signature();
		signature.setImage(data);
		signature.setLicense(license);

		// save signature
		SignatureRepository repository = new SignatureRepository(getActivity());
		repository.addSignature(signature, logEntryIds);

		//switch off signMode
		((LogEntryListActivity) getFragmentManager().findFragmentByTag(Constants.TAG_LOG_ENTRY_LIST)).toggleSignMode();
		
		// finish
		getFragmentManager().popBackStack();
	}
	
	private void clear()
	{
		DrawingView drawingView = (DrawingView)  getActivity().findViewById(R.id.SignatureDrawingView);
		drawingView.clear();
	}
	
	private void cancel()
	{
		//switch off signMode
		((LogEntryListActivity) getFragmentManager().findFragmentByTag(Constants.TAG_LOG_ENTRY_LIST)).toggleSignMode();
		
		getFragmentManager().popBackStack();
	}
}
